import time
import urllib
import urllib.request
import shutil
import os
import requests



def getdate():
    api = "http://quan.suning.com/getSysTime.do"
    getd = urllib.request.urlopen(api)
    getd =getd.read().decode('utf-8')
    getp = getd.split('\"')
    getp = getp[3]
    print("当前时间："+getp)

vers = '2.1.0'

def slp(times):
    time.sleep(times)

def WTFK():
    os.system("start https://docs.qq.com/form/page/DVVRpSWt5ZUZnUGp1")
    return

def clear():
    os.system("cls")
    return


nowpth = os.getcwd()
filepth = nowpth + '\\PCGameSDK.dll'
bpth = nowpth + '\\pkg_bf\\pkg_version'
gpth = nowpth + '\\pkg_gf\\pkg_version'


print("---------------------------SWITCHER BY SUMMER---------------------------")
print('''
   ||||||||  |||||||  |||      ||  ||||||  ||   ||  |||||  |||      ||
   ||        |||      || ||    ||  ||      ||   ||   |||   || ||    ||
   ||        |||      ||  ||   ||  ||||||  |||||||   |||   ||  ||   ||
   ||        |||||||  ||   ||  ||      ||  ||   ||   |||   ||   ||  ||
   ||    ||  |||      ||    || ||      ||  ||   ||   |||   ||    || ||
   ||||||||  |||||||  ||      |||  ||||||  ||   ||  |||||  ||      |||
''')
print("---------------------------  GENSHIN IMPACT  ---------------------------")
print("----原神 服务器互转工具 GENSHIN IMPACT SERVER SWITCHER by SUMMER-UEFI----")
print("--YS TOOL BOX 正在启动 GENSHIN IMPACT TOOLS BOX IS STARTING .........--")

time.sleep(2)
clear()
def serverq():
    try:
        fpath = open("Record.pth",'r')
    except:
        path = input("·请输入原神[启动器]路径(示例:X:\\XXX\\Genshin Impact)：")
        fpath = open("Record.pth", 'w')
        fpath.write(path)
        fpath.close()
    else:
        ysno = input("··检测到本地路径!输入T将读取保存的路径值，输入F则手动输入。··\n~~请输入：")
        if(ysno == 'T'):
            dpath = open("Record.pth", 'r')
            path = dpath.readline()
        else:
            path = input("·请输入原神游戏路径(示例:X:\\XXX\\Genshin Impact)：")
            fpath = open("Record.pth", 'w')
            fpath.write(path)
            fpath.close()
            print("更改成功！已替换本地的数值。")
            time.sleep(2)

    print("请确认游戏路径为：", path)
    print('path:',path)
    global gamestart
    gamestart = path + '\\Genshin Impact Game\\Yuanshen.exe'
    pkgpath = path + '\\Genshin Impact Game\\pkg_version'
    wheregame = path + '\\Config.ini'
    t = open(wheregame,'r') #通过INI获取path
    faq = t.readlines()
    faq = faq[1]
    faq = faq.split('=')
    faq = faq[1]
    faq = faq.split('\n')
    faq = faq[0]
    print(faq)
    path = faq + "\\Config.ini"  # 当前的path事game下的config INI
    gotopth = faq + '\\YuanShen_Data\\Plugins'  # gotopath:文件复制路劲

    try:
        file = open(path,'r')
    except:
        print("·找不到Config.ini。请重试")
        time.sleep(5)
        exit()
    else:
        print("·已读取到Config.ini，可以进行转换服务器。")
        Linef = file.readlines()
        cps = Linef[2]
        flag = 1
        if "mihoyo" in cps:
            print("·您当前的游戏版本为：Mihoyo官方服务器")
            flag = 0
        else:
            print('·您当前的游戏版本为：Bilibili服务器/未知服务器，将转换为官方服务器')
            flag = 1
        print("·接下来，输入T将会将您的服务器转换。输入F则退出。（输入T后，官服转为B服，B服转为官服）")
        runornot = input("请输入：")
        if(runornot == 'T'):
            print("请稍候！")
            if(flag == 1):
                fl = open(path,'w')
                Linef[2] = 'cps=mihoyo\n'
                Linef[1] = 'channel=1\n'
                for i in range(len(Linef)):
                    fl.write(Linef[i])
                fl.close()
                tmpgt = gotopth + '\\PCGameSDK.dll'
                os.remove(tmpgt)
                shutil.copyfile(gpth, pkgpath)
                print("··b服-->官服 ✔··")
            else:
                fl = open(path, 'w')
                Linef[2] = 'cps=bilibili\n'
                Linef[1] = 'channel=14\n'
                for i in range(len(Linef)):
                    fl.write(Linef[i])
                fl.close()
                shutil.copyfile(bpth, pkgpath)
                shutil.copy(filepth, gotopth)
                print("··官服-->b服 ✔··")
            print("转换完成！输入S一键启动原神，输入Q退出！！")
            ysorno = input('请输入：')
            if(ysorno == 'S'):
                gamestart = '\"' + gamestart + '\"'
                os.startfile(gamestart)
                return
            else:
                return
            clear()
            return
        else:
            return

def getsb():
    print("你想多了。根本没有高级功能")
    time.sleep(5)
    exit()
def mup():
    os.system("start https://ccjjyy.lanzoum.com/b0eg44txg?password=b2tt")
    return
def menu():
    print("------------------------主菜单------------------------")
    print(" 1.转服功能            2.清屏              3.查看使用方式")
    print(" 6.问题反馈 7.作者主页 8.日志                 9.退出")
    print("----------------------主菜单----------------------")
    print("软件编译时间：2023-1-30 17:32:54； 原神版本：3.4")
    getdate()
    anti = input("·请输入：")
    if (anti == '1'):
        serverq()
    elif (anti == '2'):
        clear()
    elif (anti == '3'):
        os.system('start https://www.bilibili.com/video/BV19B4y1k7ZK')
    elif (anti == '6'):
        WTFK()
    elif (anti == '7'):
        os.system('start https://space.bilibili.com/439227985/')
    elif (anti == '8'):
        os.system('start https://docs.qq.com/doc/DVXJ6Y0x6bEhXeGNa')
    elif (anti == '9'):
        exit()
    else:
        print("?")
        time.sleep(1)
        clear()
        menu()
menu()


'''
    -----------------------------------
    POWERED BY BILIBILI @ SUMMER-UEFI
    版本2.1.0
    原神PC官服B服转换器

    GENSHIN IMPACT GAME SERVER SWITCHER
    BY BILIBILI@SUMMER-UEFI
    COPYRIGHT 2022-2023
    VERSION 2.1.0
    
    FOR YUANSHEN 3.4+
    
    NOGUI-WARNING
    -----------------------------------
'''
